﻿using LineFocus.Nikcron.Common;
using LineFocus.Nikcron.Models;
using Nickron.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace LineFocus.Nikcron.Controllers
{
    public class QuotationController : Controller
    {
        //
        // GET: /Quotation/
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult GetQuotes()
        {
            ApplicationDBContext db = new ApplicationDBContext();
            JsonResult result = new JsonResult();
            var Quotes = from q in db.Quotes.ToList()
                         select new
                         {
                             QuoteId = q.Id,
                             QuoteDate = q.QuoteDate.Value.ToShortDateString(),
                             QuoteNumber = q.QuoteNumber,
                             Stockhouse = q.Stockhouse.Name,
                             Retailer = q.Retailer.Name,
                             Status = q.QuoteStatus,
                             Details = new { Product = q.Products.Select(p=>p.ProductModel.Manufacture.Name + " " + p.ProductModel.ModelNumber),
                                             UnitPrice = q.Products.Select(p=>p.UnitPrice.Amount),
                                             Qty   = q.Products.Select(p=>p.Qty),
                                             LineTotal = q.Products.Select(p => p.QuotedAmount.Amount)
                                            },
                            TotalQtys = q.Products.Sum(p=>p.Qty),
                            TotalAmt  = q.Products.Sum(p=>p.QuotedAmount.Amount)
                         };
            result.Data = Quotes;
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;
        }

        [HttpGet]
        public ActionResult GetQuoteDetails(int? QuoteId)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            JsonResult result = new JsonResult();
            var Quotes = from q in db.QuoteDetails.Where(q=>q.QuoteId == QuoteId).ToList()
                         select new
                         {
                             Brand = q.ProductModel.Manufacture.Name,
                             ProductType = q.ProductModel.ProductType.Name,
                             Model = q.ProductModel.ModelName,
                             Qty = q.Qty
                         };
            result.Data = Quotes;
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;
        }

        [HttpGet]
        public ActionResult Maintenance()
        {
            Lookup Lookup_Brands = new Lookup();
            Lookup Lookup_Models = new Lookup();
            Lookup Lookup_ProductTypes = new Lookup();
            Lookup Lookup_Stockhouses = new Lookup();
            Lookup Lookup_Retailers = new Lookup();

            ApplicationDBContext db = new ApplicationDBContext();

            var query1 = from records in db.Manufactures
                         select new LookupItem() { Id = records.Id, Name = records.Name };
            Lookup_Brands.Items = query1.ToList();

            var query2 = from records in db.ProductTypes
                         select new LookupItem() { Id = records.Id, Name = records.Name };
            Lookup_ProductTypes.Items = query2.ToList();

            var query3 = from records in db.ProductModels
                         select new LookupItem() { Id = records.Id, Name = records.ModelNumber, ParentId = records.ManufactureId };
            Lookup_Models.Parent = "Manufacture";
            Lookup_Models.Items = query3.ToList();

            var query4 = from records in db.Offices.OfType<Stockhouse>().ToList()
                         select new LookupItem() { Id = records.Id, Name = records.Name };
            Lookup_Stockhouses.Items = query4.ToList();

            var query5 = from records in db.Offices.OfType<Retailer>().ToList()
                         select new LookupItem() { Id = records.Id, Name = records.Name, ParentId = records.Dealer.Distributor.StockhouseId };
            Lookup_Retailers.Parent = "Stockhouse";
            Lookup_Retailers.Items = query5.ToList();

            ViewBag.Brands = Lookup_Brands;
            ViewBag.Models = Lookup_Models;
            ViewBag.ProductTypes = Lookup_ProductTypes;
            ViewBag.Stockhouses = Lookup_Stockhouses;
            ViewBag.Retailers = Lookup_Retailers;
            return View();
        }

        [HttpPost]
        public ActionResult Maintenance(FormCollection formCollection)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            Quote Quote = new Quote();
            Parameters QuoteNumber = db.Parameters.Where(p => p.Id == (int)ParameterTypes.Quote).FirstOrDefault();
            Quote.QuoteNumber = QuoteNumber.Number;
            Quote.StockhouseId = Int32.Parse(formCollection["Stockhouse"]);
            Quote.RetailerId = Int32.Parse(formCollection["Retailer"]);
            Quote.QuoteDate = DateTime.Parse(formCollection["QuoteDate"]);
            QuoteNumber.Number++;
            db.Quotes.AddObject(Quote);
            db.SaveChanges();

            string[] ModelNos = formCollection["ModelNos"].Split(new char[] {','}).ToArray();
            string[] Qtys = formCollection["Qtys"].Split(new char[] { ',' }).ToArray();
            string[] UnitPrices = formCollection["UnitPrices"].Split(new char[] { ',' }).ToArray();
            for(int i=0;i<ModelNos.Length;i++)
            {
                QuoteDetails QuoteDetail = new QuoteDetails();
                QuoteDetail.QuoteId = Quote.Id;
                QuoteDetail.ProductModelId = Int32.Parse(ModelNos[i]);
                QuoteDetail.UnitPrice.Currency = Currencies.INR.ToString();
                QuoteDetail.UnitPrice.Amount = double.Parse(UnitPrices[i]);
                QuoteDetail.Qty = Int32.Parse(Qtys[i]);
                QuoteDetail.QuotedAmount.Currency = Currencies.INR.ToString();
                QuoteDetail.QuotedAmount.Amount = QuoteDetail.Qty * QuoteDetail.UnitPrice.Amount;
                db.QuoteDetails.AddObject(QuoteDetail);
            }
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}
